package com.caojing.haolemai.ui.cart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.db.CartBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by CaoJing on 2016/10/23.
 */
public class CartLvAdapter extends BaseAdapter {
    private List<CartBean> list;
    private Context mContext;

    public CartLvAdapter(List<CartBean> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int i) {
        return list!=null?list.get(i):null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.collect_lv_item, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        } else{
            holder= (ViewHolder) convertView.getTag();
        }
        Glide.with(mContext).load(list.get(position)
                .getUrl()).thumbnail(0.1f).centerCrop()
                .placeholder(R.mipmap.global_default_image_outlets)
                .error(R.mipmap.global_default_image_outlets)
                .into(holder.collectLvItemIv);
        holder.collectLvItemTvName.setText(list.get(position).getName());
        holder.collectLvItemTvPrice.setText("￥"+list.get(position).getPrice());
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.collect_lv_item_iv)
        ImageView collectLvItemIv;
        @Bind(R.id.collect_lv_item_tvName)
        TextView collectLvItemTvName;
        @Bind(R.id.collect_lv_item_tvPrice)
        TextView collectLvItemTvPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
